/*
 * @Author: LVGRAPE
 * @LastEditors: LVGRAPE
 */
/*
 * Copyright (c) 2006-2025, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-06-05     RealThread   first version
 */

#include <rtthread.h>
#include <board.h>
#include <drv_common.h>

#define EOPB0_0_SRAM_64K 0
#define EOPB0_1_SRAM_16K 1
#define EOPB0_2_SRAM_64K 2
#define EOPB0_3_SRAM_32K 3

void hw_board_init(char *clock_src, int32_t clock_src_freq, int32_t clock_target_freq);

void extend_sram(void)
{
    // check if RAM has been set to 224K, if not, change EOPB0
    if (((USD->eopb0) & 0xFF) != 0XFE)
    {
        /* Unlock Option Bytes Program Erase controller */
        flash_unlock();
        /* Erase Option Bytes */
        flash_user_system_data_erase();
        /* Change SRAM size to 224KB */
        flash_user_system_data_program((uint32_t)&USD->eopb0, 0XFE);
        NVIC_SystemReset();
    }
}

RT_WEAK void rt_hw_board_init()
{
    SCB->VTOR = 0x08002800;
    /* Heap initialization */
#if defined(RT_USING_HEAP)
    rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
#endif

    hw_board_init(BSP_CLOCK_SOURCE, BSP_CLOCK_SOURCE_FREQ_MHZ, BSP_CLOCK_SYSTEM_FREQ_MHZ);

    extern int rt_hw_console_init(void);
    rt_hw_console_init();
    /* Set the shell console output device */
#if defined(RT_USING_DEVICE) && defined(RT_USING_CONSOLE)
    rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif

    /* Board underlying hardware initialization */
#ifdef RT_USING_COMPONENTS_INIT
    rt_components_board_init();
#endif
}
